package com.bosui.ycweb;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.text.InputType;
import android.text.method.DigitsKeyListener;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class SettingsActivity extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);

        PrefUtil.init(getApplicationContext());

        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        Button jump_settings = findViewById(R.id.jump_settings);
        Button ip_setting_BR300 = findViewById(R.id.ip_setting_BR300);
        Button ip_setting_location = findViewById(R.id.ip_setting_location);
        Button go_launch = findViewById(R.id.go_launch);
        Button exit = findViewById(R.id.exit);
        jump_settings.setOnClickListener(this);
        ip_setting_BR300.setOnClickListener(this);
        ip_setting_location.setOnClickListener(this);
        exit.setOnClickListener(this);
        go_launch.setOnClickListener(this);

        //显示返回按钮
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setHomeButtonEnabled(true);
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        //返回按钮点击事件
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();

        if (id == R.id.jump_settings) {
            Intent intent = new Intent(Settings.ACTION_SETTINGS);
            intent.putExtra("extra_prefs_show_button_bar", true);//是否显示button bar
            intent.putExtra("extra_prefs_set_next_text", "完成");
            intent.putExtra("extra_prefs_set_back_text", "返回");
            startActivity(intent);

        } else if (id == R.id.ip_setting_BR300) {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            EditText editText = new EditText(this);
            String ip = PrefUtil.getDefault().getString("ip", MainActivity.url_net);
            editText.setText(ip);
//            editText.setInputType(InputType.TYPE_CLASS_NUMBER | InputType
//            .TYPE_NUMBER_FLAG_DECIMAL);
            editText.setKeyListener(DigitsKeyListener.getInstance("0123456789."));
            builder.setTitle("请输入采集器IP地址");
            builder.setView(editText);
            builder.setPositiveButton("确定", null);
            AlertDialog alertDialog = builder.create();
            alertDialog.show();
            alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String ip = editText.getText().toString().trim();
                    if (isCorrectIp2(ip)) {
                        PrefUtil.getDefault().saveString("ip", ip);
                        alertDialog.cancel();
                        setResult(2);
                    } else {
                        Toast.makeText(SettingsActivity.this, "无效的IP地址", Toast.LENGTH_SHORT).show();
                    }
                }
            });

        } else if (id == R.id.ip_setting_location) {
            Intent intent = new Intent(Settings.ACTION_AIRPLANE_MODE_SETTINGS);
            intent.putExtra("extra_prefs_show_button_bar", true);//是否显示button bar
            intent.putExtra("extra_prefs_set_next_text", "完成");
            intent.putExtra("extra_prefs_set_back_text", "返回");
            startActivity(intent);
        } else if (id == R.id.exit) {
            System.exit(0);
        } else if (id == R.id.go_launch) {
            Intent homeIntent = new Intent(Intent.ACTION_MAIN);
            homeIntent.addCategory(Intent.CATEGORY_HOME);
            homeIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            homeIntent.setPackage("com.android.launcher3");
            startActivity(homeIntent);
        }


    }

    public boolean isCorrectIp2(String ipString) {
        String ipRegex = "\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}";    //IP地址的正则表达式
        //如果前三项判断都满足，就判断每段数字是否都位于0-255之间
        if (ipString.matches(ipRegex)) {
            String[] ipArray = ipString.split("\\.");
            for (int i = 0; i < ipArray.length; i++) {
                int number = Integer.parseInt(ipArray[i]);
                //4.判断每段数字是否都在0-255之间
                if (number < 0 || number > 255) {
                    return false;
                }
            }
            return true;
        } else {
            return false;    //如果与正则表达式不匹配，则返回false
        }
    }
}